package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	
	public class PurchaseDialog extends Sprite
	{
    	private var numSel:NumberSelecter;
    	private var objectConfig:ObjectConfig;
    	private var type:int;
    	private var honourBalance:String;

    	//type = 0 买, type = 1 卖
		public function PurchaseDialog(objectConfig:ObjectConfig, type:int)
		{
			super();
			this.type = type;
			this.honourBalance = "";
			this.objectConfig = objectConfig;
			//Add main frame
			var PurchaseDialog:Class = ResourceHelper.getInstance().getClassByName("MessageBox1");
			var dialogSprite:Sprite = new PurchaseDialog();
			addChild(dialogSprite);
			
			//Add image
			var ClassImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage);
			var imageSprite:Sprite = new ClassImage();
			imageSprite.width = 60;
			imageSprite.height = 60;
			imageSprite.x = 100;
			imageSprite.y = 55;
			addChild(imageSprite);
			
			//加载名字
			var name:TextField = new TextField();
			name.selectable = false;
			name.autoSize = TextFieldAutoSize.LEFT;
			name.wordWrap = true;
			name.defaultTextFormat = new TextFormat("微软雅黑", 16, 0x000000, true);
			name.text = objectConfig.name;
			name.x = 190 - name.width;
			name.y = 115;
			addChild(name);
			
			//加载价格图标
			var PriceImage:Class;
			var price:int;
			if (objectConfig.priceGoldBones > 0)
			{
				PriceImage = ResourceHelper.getInstance().getClassByName("PriceGoldBone");
				if (type == 0)
					price = objectConfig.priceGoldBones;
				else
					price = Math.ceil(objectConfig.priceGoldBones * Config.disCount);
			}
			else 
			{
				PriceImage = ResourceHelper.getInstance().getClassByName("PriceBone");
				if (type == 0)
					price = objectConfig.priceBones;
				else
					price = Math.ceil(objectConfig.priceBones * Config.disCount);
			}
			var priceImage:Sprite = new PriceImage();
			priceImage.width = 25;
			priceImage.height = 25;
			priceImage.x = 240;
			priceImage.y = 80;
			addChild(priceImage);
			//加载价格
			var priceText:TextField = new TextField();
			priceText.selectable = false;
			priceText.autoSize = TextFieldAutoSize.LEFT;
			priceText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			priceText.text = price.toString();
			priceText.x = priceImage.x + priceImage.width + 10;
			priceText.y = priceImage.y;
			addChild(priceText);
						
			//加载具体描述
			var description:TextField = new TextField();
			description.selectable = false;
			description.autoSize = TextFieldAutoSize.LEFT;
			description.width = 200;
			description.wordWrap = true;
			description.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			description.text = objectConfig.description;
			description.x = 50;
			description.y = 150;
			addChild(description);
			
			//Add number select
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			if (type == 0)
			{
				numSel = new NumberSelecter(LeftArrowButton, RightArrowButton);
			}
			else
			{
				var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				var objectNum:int = player.userPackage.objectList[objectConfig.index];
				numSel = new NumberSelecter(LeftArrowButton, RightArrowButton, objectNum, 1, objectNum);
			}
			addChild(numSel);
			numSel.x = 210;
			numSel.y = 120;
			
			var gap:int = -60; 
			//Add OK button
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			var buttonOK:BasicButton;
			if (type == 0)
			{
				buttonOK = new BasicButton(ButtonBgr1, "购买");
			}
			else if (type == 1)
			{
				buttonOK = new BasicButton(ButtonBgr1, "出售");
			}
			buttonOK.x = (dialogSprite.width  - gap) / 2 - 3 * buttonOK.width / 2;
			buttonOK.y = dialogSprite.height - buttonOK.height - 60;
			addChild(buttonOK);
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKMouseClick);
			
			//Add Cancel button
			var buttonCancel:BasicButton = new BasicButton(ButtonBgr1, "取消");
			buttonCancel.x = (dialogSprite.width  + gap) / 2 + buttonCancel.width / 2;
			buttonCancel.y = buttonOK.y;
			addChild(buttonCancel);
			buttonCancel.addEventListener(MouseEvent.CLICK, onButtonCancelMouseClick);
		}
		
		private function onButtonCancelMouseClick(event:MouseEvent):void
        {
        	this.parent.removeChild(this);
        }
        
		private function onButtonOKMouseClick(event:MouseEvent):void
        {
    		//购买
        	if (type == 0)
        	{
        		var player:PlayerInfo = PlayerInfoHelper.getInstance().players[PlayerInfoHelper.getInstance().userId];
				if ((player.userPackage.objectList[objectConfig.index] == null ||player.userPackage.objectList[objectConfig.index] <= 0) && objectConfig.type == 2)
				{
					var equipConfig:EquipConfig = objectConfig as EquipConfig;
					if (player.checkNewHonour(objectConfig.index, null))
						honourBalance = PlayerInfoHelper.getInstance().userId + "," + equipConfig.equipType + "," + Config.equipColorScore[equipConfig.color] + ",";
				}
        		ServerHelper.getInstance().purchaseObject(objectConfig.index, numSel.value, objectConfig.priceBones, objectConfig.priceGoldBones, onPurchaseObjectComplete, honourBalance);
        	}
    		//卖出
        	else if (type == 1)
        	{
        		ServerHelper.getInstance().sellObject(objectConfig.index, numSel.value, Math.ceil(objectConfig.priceBones * Config.disCount) , objectConfig.priceGoldBones, onSellObjectComplete);
        	}
        }
        
        private function onSellObjectComplete(result:String):void
        {
        	var messageText:String;
        	if (result == "success")
        	{
        		messageText = "出售成功！";
        	}
        	else if (result == "not enough object")
        	{
        		messageText = "对不起，您没有足够的物品！";
        	}
        	var msgBox:MessageBox = new MessageBox(messageText);
        	msgBox.x = (stage.stageWidth - msgBox.width) / 2;
        	msgBox.y = (stage.stageHeight - msgBox.height) / 2;
        	SceneContainer.getInstance().currentScene.addChild(msgBox);
        	BottomNavigator.getInstance().refresh();
        	this.parent.removeChild(this);
        }
        
        private function onPurchaseObjectComplete(result:String, newHonours:Object):void
        {
        	//点亮新的勋章
        	if (newHonours != null)
        	{
        		var honorBox:HonourBox = new HonourBox(newHonours);
        		honorBox.x = (BFont.stageWidth - honorBox.width) / 2;
        		honorBox.y = (BFont.stageHeight - honorBox.height) / 2;
        		SceneContainer.getInstance().currentScene.addChild(honorBox);
        	}
        	else
        	{
	        	var messageText:String;
	        	if (result == "success")
	        	{
	        		messageText = "购买成功，请到包裹中查看！";
	        	}
	        	else if (result == "not enough bones")
	        	{
	        		messageText = "没有足够的骨头用于购买！";
	        	}
	        	else if (result == "not enough goldbones")
	        	{
	        		messageText = "没有足够的金骨头用于购买！";
	        	}
	        	var msgBox:MessageBox = new MessageBox(messageText);
	        	msgBox.x = (stage.stageWidth - msgBox.width) / 2;
	        	msgBox.y = (stage.stageHeight - msgBox.height) / 2;
	        	SceneContainer.getInstance().currentScene.addChild(msgBox);
	        }
        	close();
        }
        
        public function close():void
        {
        	if (this.parent != null)
        		this.parent.removeChild(this);
        }
	}
}